/**
 * Author: 老洋
 * Date:  2021/3/28 16:16
 */
package com.yyy.dao;


import com.yyy.pojo.Department;
import com.yyy.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

//员工Dao层
@Repository
public class EmployeeDao {
    public static Map<Integer, Employee> employees = null;
    public static int empId = 1006;

    @Autowired
    public DepartmentDao departmentDao = null;

    static {
        employees = new HashMap<Integer, Employee>();

        employees.put(1001, new Employee(1001, "AAyang", "AA2560055298@qq.com", 1, new Department(101, "教学部")));
        employees.put(1002, new Employee(1002, "BByang", "BB2560055298@qq.com", 0, new Department(102, "学工部")));
        employees.put(1003, new Employee(1003, "CCyang", "CC2560055298@qq.com", 1, new Department(103, "教务处")));
        employees.put(1004, new Employee(1004, "DDyang", "DD2560055298@qq.com", 0, new Department(104, "财务部")));
        employees.put(1005, new Employee(1005, "EEyang", "EE2560055298@qq.com", 1, new Department(105, "人事部")));
    }

    //1-1、查询员工信息
    public Collection<Employee> selAllEmploy(){
        return employees.values();
    }

    //1-2、通过ID查询员工信息
    public Employee selEmployById(Integer id){
        return employees.get(id);
    }


    //2、添加员工信息
    public void insEmploy(Employee e){
        if(e != null){
            //获取部门ID
            int key =  e.getDepartment().getId();

            //设置部门信息
            e.setDepartment(departmentDao.getDepartmentById(key));
            //e.setId(empId++);
           // employees.put(empId, e);

            if(e.getId() == null){
                e.setId(empId++);
            }
            employees.put(e.getId(), e);
        }
    }

    //3、更新员工信息
    public void updEmploy(Employee e){
        if(e != null){
            employees.put(e.getId(),e);
        }
    }

    //4、删除员工信息
    public void delEmploy(Integer id){
        employees.remove(id);
    }
}